package com.guodu.dao.impl;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.guodu.base.BaseDao;
import com.guodu.dao.PurchasingInformationDao;
import com.guodu.entity.PurchasingInformation;
import com.guodu.entity.PurchasinginforStatus;
import com.guodu.entity.Units;
import com.guodu.entity.UserInfo;
import com.guodu.vo.PurchasingInformationQueryVo;
import com.guodu.vo.PurchasingInformationVo;



@Repository("purchasingInformationDao")
public class PurchasingInformationDaoImpl extends BaseDao implements PurchasingInformationDao {

	@Override
	public long findSizeByVo(PurchasingInformationQueryVo piq, Units units,UserInfo userInfo) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("select count(*) from PurchasingInformation m where 1=1");
		if(piq!=null){
			if(piq.getFunitsId()!=null){
				hql.append("    and (exists(select 1 from Units u where u.unitsId = m.unitsId and u.funitsId="+piq.getFunitsId()+" ) or m.unitsId ="+piq.getFunitsId()+" )");
			}
			if(piq.getUnitsId()!=null && !"".equals(piq.getUnitsId())){
				hql.append(" and  m.unitsId = "+piq.getUnitsId()+"");
			}
			if (piq.getProjectName()!=null && !"".equals(piq.getProjectName())) {
				hql.append(" and projectName like '%"+piq.getProjectName()+"%' ");
			}
			if (piq.getProjectType()!=null && !"".equals(piq.getProjectType())) {
				hql.append(" and projectType like '%"+piq.getProjectType()+"%' ");
			}
			if (piq.getProjectSituation()!=null && !"".equals(piq.getProjectSituation())) {
				hql.append(" and projectSituation like '%"+piq.getProjectSituation()+"%' ");
			}
			if (piq.getSectionName()!=null && !"".equals(piq.getSectionName())) {
				hql.append(" and sectionName like '%"+piq.getSectionName()+"%' ");
			}
			if (piq.getSecName()!=null && !"".equals(piq.getSecName())) {
				hql.append(" and secName like '%"+piq.getSecName()+"%' ");
			}
			if (piq.getSecUnits()!=null && !"".equals(piq.getSecUnits())) {
				hql.append(" and secUnits like '%"+piq.getSecUnits()+"%' ");
			}
			if (piq.getSecClassification()!=null && !"".equals(piq.getSecClassification())) {
				hql.append(" and secClassification like '%"+piq.getSecClassification()+"%' ");
			}
			if (piq.getProcurementMethod()!=null && !"".equals(piq.getProcurementMethod())) {
				hql.append(" and procurementMethod like '%"+piq.getProcurementMethod()+"%' ");
			}
			if(piq.getCreateBeginTime()!=null&&piq.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+piq.getCreateBeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(piq.getCreateEndTime()!=null&&piq.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+piq.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getFcbeginTime()!=null && !"".equals(piq.getFcbeginTime())) {
				hql.append("    and issueTime >= TO_DATE('"+piq.getFcbeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getFcendTime()!=null && !"".equals(piq.getFcendTime())) {
				hql.append("    and issueTime <= TO_DATE('"+piq.getFcendTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getReviewTimeBegin()!=null && !"".equals(piq.getReviewTimeBegin())) {
				hql.append("    and reviewTime >= TO_DATE('"+piq.getReviewTimeBegin()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getReviewTimeEnd()!=null && !"".equals(piq.getReviewTimeEnd())) {
				hql.append("    and reviewTime <= TO_DATE('"+piq.getReviewTimeEnd()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(piq.getStatus()!=null&&piq.getStatus().trim().length()>0&&!piq.getStatus().equals("5")){
				hql.append(" and status ="+piq.getStatus());
			}
			if (piq.getPurchaseYears()!=null && !"".equals(piq.getPurchaseYears())) {
				hql.append("    and TO_DATE(purchaseYears,'yyyy-mm') >= TO_DATE('"+piq.getPurchaseYears()+"','yyyy-mm')");
			}
			if (piq.getEpurchaseYears()!=null && !"".equals(piq.getEpurchaseYears())) {
				hql.append("    and TO_DATE(purchaseYears,'yyyy-mm') <= TO_DATE('"+piq.getEpurchaseYears()+"','yyyy-mm')");
			}
		}
		if(units==null){
			hql.append(" and (status=1 or status=3 or status=4 or (status=0 and exists(select 1 from UserInfo u where u.userId=m.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
		}else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1){
			hql.append(" and createPeople = "+userInfo.getUserId());
		}else if (units.getIsComplete()==0L){
			hql.append(" and (status=1 or status=3)");
		}
		return super.findInt(hql.toString());
	}

	@Override
	public void examinAll(PurchasingInformationQueryVo piq, Units units,
			UserInfo userInfo) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer();
		if(units==null){
			hql.append("update from PurchasingInformation m set m.status=3 where m.status=0 ");
		}else if(units.getUnitsLevel()==1L){
			hql.append("update from PurchasingInformation m set m.status=1 where m.status=0 ");
		}
		if(piq!=null){
			if(piq.getFunitsId()!=null){
				hql.append("    and (exists(select 1 from Units u where u.unitsId = m.unitsId and u.funitsId="+piq.getFunitsId()+" ) or m.unitsId ="+piq.getFunitsId()+" )");
			}
			if(piq.getUnitsId()!=null && !"".equals(piq.getUnitsId())){
				hql.append(" and  m.unitsId = "+piq.getUnitsId()+"");
			}
			if (piq.getProjectName()!=null && !"".equals(piq.getProjectName())) {
				hql.append(" and projectName like '%"+piq.getProjectName()+"%' ");
			}
			if (piq.getProjectType()!=null && !"".equals(piq.getProjectType())) {
				hql.append(" and projectType like '%"+piq.getProjectType()+"%' ");
			}
			if (piq.getProjectSituation()!=null && !"".equals(piq.getProjectSituation())) {
				hql.append(" and projectSituation like '%"+piq.getProjectSituation()+"%' ");
			}
			if (piq.getSectionName()!=null && !"".equals(piq.getSectionName())) {
				hql.append(" and sectionName like '%"+piq.getSectionName()+"%' ");
			}
			if (piq.getSecName()!=null && !"".equals(piq.getSecName())) {
				hql.append(" and secName like '%"+piq.getSecName()+"%' ");
			}
			if (piq.getSecUnits()!=null && !"".equals(piq.getSecUnits())) {
				hql.append(" and secUnits like '%"+piq.getSecUnits()+"%' ");
			}
			if (piq.getSecClassification()!=null && !"".equals(piq.getSecClassification())) {
				hql.append(" and secClassification like '%"+piq.getSecClassification()+"%' ");
			}
			if (piq.getProcurementMethod()!=null && !"".equals(piq.getProcurementMethod())) {
				hql.append(" and procurementMethod like '%"+piq.getProcurementMethod()+"%' ");
			}
			if(piq.getCreateBeginTime()!=null&&piq.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+piq.getCreateBeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(piq.getCreateEndTime()!=null&&piq.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+piq.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getFcbeginTime()!=null && !"".equals(piq.getFcbeginTime())) {
				hql.append("    and issueTime >= TO_DATE('"+piq.getFcbeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getFcendTime()!=null && !"".equals(piq.getFcendTime())) {
				hql.append("    and issueTime <= TO_DATE('"+piq.getFcendTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getReviewTimeBegin()!=null && !"".equals(piq.getReviewTimeBegin())) {
				hql.append("    and reviewTime >= TO_DATE('"+piq.getReviewTimeBegin()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getReviewTimeEnd()!=null && !"".equals(piq.getReviewTimeEnd())) {
				hql.append("    and reviewTime <= TO_DATE('"+piq.getReviewTimeEnd()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(piq.getStatus()!=null&&piq.getStatus().trim().length()>0&&!piq.getStatus().equals("5")){
				hql.append(" and status ="+piq.getStatus());
			}
			if (piq.getPurchaseYears()!=null && !"".equals(piq.getPurchaseYears())) {
				hql.append("    and TO_DATE(purchaseYears,'yyyy-mm') >= TO_DATE('"+piq.getPurchaseYears()+"','yyyy-mm')");
			}
			if (piq.getEpurchaseYears()!=null && !"".equals(piq.getEpurchaseYears())) {
				hql.append("    and TO_DATE(purchaseYears,'yyyy-mm') <= TO_DATE('"+piq.getEpurchaseYears()+"','yyyy-mm')");
			}
		}
		if(units==null){
			hql.append(" and (status=1 or status=3 or status=4 or (status=0 and exists(select 1 from UserInfo u where u.userId=m.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
		}else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1){
			hql.append(" and createPeople = "+userInfo.getUserId());
		}else if (units.getIsComplete()==0L){
			hql.append(" and (status=1 or status=3)");
		}
		super.executeUpdate(hql.toString());
		
	}

	@Override
	public void save(PurchasingInformation purchasingInformation) {
		// TODO Auto-generated method stub
		super.save(purchasingInformation);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PurchasingInformation> findByVo(
			PurchasingInformationQueryVo piq, Units units,UserInfo userInfo, int curPage,
			int pageSize) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from PurchasingInformation m where 1=1");
		if(piq!=null){
			if(piq.getFunitsId()!=null){
				hql.append("    and (exists(select 1 from Units u where u.unitsId = m.unitsId and u.funitsId="+piq.getFunitsId()+" ) or m.unitsId ="+piq.getFunitsId()+" )");
			}
			if(piq.getUnitsId()!=null && !"".equals(piq.getUnitsId())){
				hql.append(" and  m.unitsId = "+piq.getUnitsId()+"");
			}
			if (piq.getProjectName()!=null && !"".equals(piq.getProjectName())) {
				hql.append(" and projectName like '%"+piq.getProjectName()+"%' ");
			}
			if (piq.getProjectType()!=null && !"".equals(piq.getProjectType())) {
				hql.append(" and projectType like '%"+piq.getProjectType()+"%' ");
			}
			if (piq.getProjectSituation()!=null && !"".equals(piq.getProjectSituation())) {
				hql.append(" and projectSituation like '%"+piq.getProjectSituation()+"%' ");
			}
			if (piq.getSectionName()!=null && !"".equals(piq.getSectionName())) {
				hql.append(" and sectionName like '%"+piq.getSectionName()+"%' ");
			}
			if (piq.getSecName()!=null && !"".equals(piq.getSecName())) {
				hql.append(" and secName like '%"+piq.getSecName()+"%' ");
			}
			if (piq.getSecUnits()!=null && !"".equals(piq.getSecUnits())) {
				hql.append(" and secUnits like '%"+piq.getSecUnits()+"%' ");
			}
			if (piq.getSecClassification()!=null && !"".equals(piq.getSecClassification())) {
				hql.append(" and secClassification like '%"+piq.getSecClassification()+"%' ");
			}
			if (piq.getProcurementMethod()!=null && !"".equals(piq.getProcurementMethod())) {
				hql.append(" and procurementMethod like '%"+piq.getProcurementMethod()+"%' ");
			}
			if(piq.getCreateBeginTime()!=null&&piq.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+piq.getCreateBeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(piq.getCreateEndTime()!=null&&piq.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+piq.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getFcbeginTime()!=null && !"".equals(piq.getFcbeginTime())) {
				hql.append("    and issueTime >= TO_DATE('"+piq.getFcbeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getFcendTime()!=null && !"".equals(piq.getFcendTime())) {
				hql.append("    and issueTime <= TO_DATE('"+piq.getFcendTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getReviewTimeBegin()!=null && !"".equals(piq.getReviewTimeBegin())) {
				hql.append("    and reviewTime >= TO_DATE('"+piq.getReviewTimeBegin()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getReviewTimeEnd()!=null && !"".equals(piq.getReviewTimeEnd())) {
				hql.append("    and reviewTime <= TO_DATE('"+piq.getReviewTimeEnd()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(piq.getStatus()!=null&&piq.getStatus().trim().length()>0&&!piq.getStatus().equals("5")){
				hql.append(" and status ="+piq.getStatus());
			}
			if (piq.getPurchaseYears()!=null && !"".equals(piq.getPurchaseYears())) {
				hql.append("    and TO_DATE(purchaseYears,'yyyy-mm') >= TO_DATE('"+piq.getPurchaseYears()+"','yyyy-mm')");
			}
			if (piq.getEpurchaseYears()!=null && !"".equals(piq.getEpurchaseYears())) {
				hql.append("    and TO_DATE(purchaseYears,'yyyy-mm') <= TO_DATE('"+piq.getEpurchaseYears()+"','yyyy-mm')");
			}
		}
		if(units==null){
			hql.append(" and (status=1 or status=3 or status=4 or (status=0 and exists(select 1 from UserInfo u where u.userId=m.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
		}else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1){
			hql.append(" and createPeople = "+userInfo.getUserId());
		}else if (units.getIsComplete()==0L){
			hql.append(" and (status=1 or status=3)");
		}
		hql.append(" order by purchaseId desc");
		return super.findPages(hql.toString(), curPage, pageSize);
	}

	@Override
	public PurchasingInformation findById(String purchaseId) {
		// TODO Auto-generated method stub
		return (PurchasingInformation) super.findById(PurchasingInformation.class, purchaseId);
	}

	@Override
	public void update(PurchasingInformation purchasingInformation) {
		// TODO Auto-generated method stub
		super.update(purchasingInformation);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PurchasinginforStatus> findStatus(String purchaseId) {
		// TODO Auto-generated method stub
		return super.findByProperty(PurchasinginforStatus.class, "purchaseId", purchaseId);
	}

	@Override
	public void changeStatus(List<PurchasinginforStatus> entities) {
		// TODO Auto-generated method stub
		super.saveAll(entities);
	}

	@Override
	public void examine(String ids, Long status) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("update PurchasingInformation set status =");
		hql.append(status);
		hql.append(" where purchaseId in (");
		hql.append(ids);
		hql.append(")");
		super.executeUpdate(hql.toString());
	}

	@Override
	public void delByIds(String[] delIds) {
		// TODO Auto-generated method stub
		super.deleteAllById(PurchasingInformation.class, delIds);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<PurchasingInformation> findAllByVo(
			PurchasingInformationQueryVo piq, Units units,UserInfo userInfo) {
		// TODO Auto-generated method stub
		StringBuffer hql = new StringBuffer("from PurchasingInformation m where 1=1");
		if(piq!=null){
			if(piq.getFunitsId()!=null){
				hql.append("    and (exists(select 1 from Units u where u.unitsId = m.unitsId and u.funitsId="+piq.getFunitsId()+" ) or m.unitsId ="+piq.getFunitsId()+" )");
			}
			if(piq.getUnitsId()!=null && !"".equals(piq.getUnitsId())){
				hql.append(" and  m.unitsId = "+piq.getUnitsId()+"");
			}
			if (piq.getProjectName()!=null && !"".equals(piq.getProjectName())) {
				hql.append(" and projectName like '%"+piq.getProjectName()+"%' ");
			}
			if (piq.getProjectType()!=null && !"".equals(piq.getProjectType())) {
				hql.append(" and projectType like '%"+piq.getProjectType()+"%' ");
			}
			if (piq.getProjectSituation()!=null && !"".equals(piq.getProjectSituation())) {
				hql.append(" and projectSituation like '%"+piq.getProjectSituation()+"%' ");
			}
			if (piq.getSectionName()!=null && !"".equals(piq.getSectionName())) {
				hql.append(" and sectionName like '%"+piq.getSectionName()+"%' ");
			}
			if (piq.getSecName()!=null && !"".equals(piq.getSecName())) {
				hql.append(" and secName like '%"+piq.getSecName()+"%' ");
			}
			if (piq.getSecUnits()!=null && !"".equals(piq.getSecUnits())) {
				hql.append(" and secUnits like '%"+piq.getSecUnits()+"%' ");
			}
			if (piq.getSecClassification()!=null && !"".equals(piq.getSecClassification())) {
				hql.append(" and secClassification like '%"+piq.getSecClassification()+"%' ");
			}
			if (piq.getProcurementMethod()!=null && !"".equals(piq.getProcurementMethod())) {
				hql.append(" and procurementMethod like '%"+piq.getProcurementMethod()+"%' ");
			}
			if(piq.getCreateBeginTime()!=null&&piq.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+piq.getCreateBeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(piq.getCreateEndTime()!=null&&piq.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+piq.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getFcbeginTime()!=null && !"".equals(piq.getFcbeginTime())) {
				hql.append("    and issueTime >= TO_DATE('"+piq.getFcbeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getFcendTime()!=null && !"".equals(piq.getFcendTime())) {
				hql.append("    and issueTime <= TO_DATE('"+piq.getFcendTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getReviewTimeBegin()!=null && !"".equals(piq.getReviewTimeBegin())) {
				hql.append("    and reviewTime >= TO_DATE('"+piq.getReviewTimeBegin()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getReviewTimeEnd()!=null && !"".equals(piq.getReviewTimeEnd())) {
				hql.append("    and reviewTime <= TO_DATE('"+piq.getReviewTimeEnd()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(piq.getStatus()!=null&&piq.getStatus().trim().length()>0&&!piq.getStatus().equals("5")){
				hql.append(" and status ="+piq.getStatus());
			}
			if (piq.getPurchaseYears()!=null && !"".equals(piq.getPurchaseYears())) {
				hql.append("    and TO_DATE(purchaseYears,'yyyy-mm') >= TO_DATE('"+piq.getPurchaseYears()+"','yyyy-mm')");
			}
			if (piq.getEpurchaseYears()!=null && !"".equals(piq.getEpurchaseYears())) {
				hql.append("    and TO_DATE(purchaseYears,'yyyy-mm') <= TO_DATE('"+piq.getEpurchaseYears()+"','yyyy-mm')");
			}
		}
		if(units==null){
			hql.append(" and (status=1 or status=3 or status=4 or (status=0 and exists(select 1 from UserInfo u where u.userId=m.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
		}else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1){
			hql.append(" and createPeople = "+userInfo.getUserId());
		}else if (units.getIsComplete()==0L){
			hql.append(" and (status=1 or status=3)");
		}
		hql.append("order by createTime desc");
		return super.find(hql.toString());
	}

	@Override
	public PurchasingInformationVo findAllMoney(
			PurchasingInformationQueryVo piq, Units units, UserInfo userInfo) {
		StringBuffer hql = new StringBuffer("select sum(secEvaluation),sum(sectionEvaluation),sum(tenderOffer),sum(bidEvaluation),sum(saveMoney) from PurchasingInformation m where 1=1");
		if(piq!=null){
			if(piq.getFunitsId()!=null){
				hql.append("    and (exists(select 1 from Units u where u.unitsId = m.unitsId and u.funitsId="+piq.getFunitsId()+" ) or m.unitsId ="+piq.getFunitsId()+" )");
			}
			if(piq.getUnitsId()!=null && !"".equals(piq.getUnitsId())){
				hql.append(" and  m.unitsId = "+piq.getUnitsId()+"");
			}
			if (piq.getProjectName()!=null && !"".equals(piq.getProjectName())) {
				hql.append(" and projectName like '%"+piq.getProjectName()+"%' ");
			}
			if (piq.getProjectType()!=null && !"".equals(piq.getProjectType())) {
				hql.append(" and projectType like '%"+piq.getProjectType()+"%' ");
			}
			if (piq.getProjectSituation()!=null && !"".equals(piq.getProjectSituation())) {
				hql.append(" and projectSituation like '%"+piq.getProjectSituation()+"%' ");
			}
			if (piq.getSectionName()!=null && !"".equals(piq.getSectionName())) {
				hql.append(" and sectionName like '%"+piq.getSectionName()+"%' ");
			}
			if (piq.getSecName()!=null && !"".equals(piq.getSecName())) {
				hql.append(" and secName like '%"+piq.getSecName()+"%' ");
			}
			if (piq.getSecUnits()!=null && !"".equals(piq.getSecUnits())) {
				hql.append(" and secUnits like '%"+piq.getSecUnits()+"%' ");
			}
			if (piq.getSecClassification()!=null && !"".equals(piq.getSecClassification())) {
				hql.append(" and secClassification like '%"+piq.getSecClassification()+"%' ");
			}
			if (piq.getProcurementMethod()!=null && !"".equals(piq.getProcurementMethod())) {
				hql.append(" and procurementMethod like '%"+piq.getProcurementMethod()+"%' ");
			}
			if(piq.getCreateBeginTime()!=null&&piq.getCreateBeginTime().trim().length()>0){
				hql.append(" and createTime >= TO_DATE('"+piq.getCreateBeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if(piq.getCreateEndTime()!=null&&piq.getCreateEndTime().trim().length()>0){
				hql.append(" and createTime <= TO_DATE('"+piq.getCreateEndTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getFcbeginTime()!=null && !"".equals(piq.getFcbeginTime())) {
				hql.append("    and issueTime >= TO_DATE('"+piq.getFcbeginTime()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getFcendTime()!=null && !"".equals(piq.getFcendTime())) {
				hql.append("    and issueTime <= TO_DATE('"+piq.getFcendTime()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getReviewTimeBegin()!=null && !"".equals(piq.getReviewTimeBegin())) {
				hql.append("    and reviewTime >= TO_DATE('"+piq.getReviewTimeBegin()+" 00:00:00','yyyy-mm-dd hh24:mi:ss')");
			}
			if (piq.getReviewTimeEnd()!=null && !"".equals(piq.getReviewTimeEnd())) {
				hql.append("    and reviewTime <= TO_DATE('"+piq.getReviewTimeEnd()+" 23:59:59','yyyy-mm-dd hh24:mi:ss')");
			}
			if(piq.getStatus()!=null&&piq.getStatus().trim().length()>0&&!piq.getStatus().equals("5")){
				hql.append(" and status ="+piq.getStatus());
			}
			if (piq.getPurchaseYears()!=null && !"".equals(piq.getPurchaseYears())) {
				hql.append("    and TO_DATE(purchaseYears,'yyyy-mm') >= TO_DATE('"+piq.getPurchaseYears()+"','yyyy-mm')");
			}
			if (piq.getEpurchaseYears()!=null && !"".equals(piq.getEpurchaseYears())) {
				hql.append("    and TO_DATE(purchaseYears,'yyyy-mm') <= TO_DATE('"+piq.getEpurchaseYears()+"','yyyy-mm')");
			}
		}
		if(units==null){
			hql.append(" and (status=1 or status=3 or status=4 or (status=0 and exists(select 1 from UserInfo u where u.userId=m.createPeople and exists(select 1 from Units n where n.unitsId=u.unitsId and n.unitsLevel = 1) )))");
		}else if(units.getUnitsLevel()==2L&&units.getIsComplete()==1){
			hql.append(" and createPeople = "+userInfo.getUserId());
		}else if (units.getIsComplete()==0L){
			hql.append(" and (status=1 or status=3)");
		}
		PurchasingInformationVo p = new PurchasingInformationVo();
		List<Object[]> objs = super.find(hql.toString());
		if(objs.size()>0){
			DecimalFormat df = new DecimalFormat("##,###.##");
			try {
				if(objs.get(0)[0]!=null){
					p.setAllMoney1(df.format(df.parse(objs.get(0)[0].toString())));
				}
                if(objs.get(0)[1]!=null){
                	p.setAllMoney2(df.format(df.parse(objs.get(0)[1].toString())));
				}
                if(objs.get(0)[2]!=null){
                	p.setAllMoney3(df.format(df.parse(objs.get(0)[2].toString())));
				}
                if(objs.get(0)[3]!=null){
                	p.setAllMoney4(df.format(df.parse(objs.get(0)[3].toString())));
				}
                if(objs.get(0)[4]!=null){
                	p.setAllMoney5(df.format(df.parse(objs.get(0)[4].toString())));
				}
			}catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return p;
	}


	

}
